home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 974 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.8 KB  |  83 lines

  1. Path: news.sprintlink.net!datalytics!news
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Binary IOStreams?
  5. Date: 8 Jan 1996 18:59:49 GMT
  6. Organization: Datalytics, Inc
  7. Message-ID: <4crpj5$h4r@gold.datalytics.com>
  8. References: <DKny36.A0@BearRiver.com>
  9. NNTP-Posting-Host: pc071.datalytics.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
  14.  
  15. Duane Murphy <dmurphy@bearriver.com> wrote:
  16. >Are IOStreams (the ios & streambuf hieararchy) always text streams? 
  17. >Is there a way to make the become binary? Is there a seperate 
  18. >specification or hierarchy for binary streams?
  19. >
  20.  
  21. There is no version of the iostreams classes to handle 
  22. strictly binary I/O of the data.
  23.  
  24. >By binary streams I mean streams where operator>>(int) does _NOT_ 
  25. >first convert to the text of the number but just emits its binary 
  26. >representation (ie by calling streambuf::write()).
  27. >
  28.  
  29. You can effect this behavior pretty easily.  However, you must 
  30. override all of the shift operators for each of the types.  
  31. You can use ostream::write() and istream::read() to handle the 
  32. binary data.
  33.  
  34. Thus, this should do the trick.
  35.  
  36. class bistream : public istream
  37. {
  38.    public:
  39.       ...
  40.       inline
  41.       istream& operator >>(
  42.                   int& o_Number);
  43.       ...
  44. };
  45.  
  46. inline
  47. istream& istream::operator >>(
  48.    int& o_Number)
  49. {
  50.    read(
  51.       (char*)&o_Number,
  52.       sizeof(o_Number));
  53.    return *this;
  54. }
  55.  
  56. class bostream : public ostream
  57. {
  58.    public:
  59.       ...
  60.       inline
  61.       ostream& operator <<(
  62.                   int i_Number);
  63.       ...
  64. };
  65.  
  66. inline
  67. ostream& ostream::operator <<(
  68.    int& i_Number)
  69. {
  70.    write(
  71.       (char*)&i_Number,
  72.       sizeof(i_Number));
  73.    return *this;
  74. }
  75.  
  76. -- 
  77. Robert Stewart        | My opinions are usually my own.
  78. Datalytics, Inc.
  79. (513)226-7700
  80. stew@datalytics.com
  81.  
  82.  
  83.